package saxion.speelveld.spaceengineer.controller;

import java.util.ArrayList;

import saxion.speelveld.spaceengineer.R;
import saxion.speelveld.spaceengineer.model.ResourceType;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class BitmapManager
{
	private static ArrayList<BitmapLayout> bitmaps = new ArrayList<BitmapLayout>();

	public static Bitmap getBitmapFromResources(int id, Context context)
	{
		// Log.d("getBitmapFromResources", "start" + id);
		// for (BitmapLayout bitmapItem : bitmaps)
		// {
		// if (bitmapItem.id == id)
		// {
		// return bitmapItem.bitmap;
		// }
		// }

		// does not yet exist.
		// BitmapLayout bitmaplayout = new BitmapLayout();
		// bitmaplayout.id = id;
		if (context.getResources().getDrawable(id) instanceof BitmapDrawable)
		{
			return ((BitmapDrawable) context.getResources().getDrawable(id)).getBitmap();
		}
		else
		{
			return null;
		}
		// bitmaplayout.bitmap = BitmapFactory.decodeResource(context.getResources(), id);
		// bitmaplayout.bitmap = ((BitmapDrawable) context.getResources().getDrawable(id)).getBitmap();

		// bitmaps.add(bitmaplayout);
		// Log.d("getBitmapFromResources", "end" + id);
		// return bitmaplayout.bitmap;
	}

	public static Bitmap getBitmapByResourceType(ResourceType resourceType, Context context)
	{
		int resId = getResourceIdForResourceType(resourceType);
		return getBitmapFromResources(resId, context);
	}

	public static Drawable getDrawableByResourceType(ResourceType resourceType, Context context)
	{
		int resId = getResourceIdForResourceType(resourceType);
		return context.getResources().getDrawable(resId);
	}

	private static int getResourceIdForResourceType(ResourceType resourceType)
	{
		int resId = 0;
		switch (resourceType)
		{
		case CLOTHING:
			resId = R.drawable.clothing;
			break;
		case COTTON:
			resId = R.drawable.cotton;
			break;
		case ELECTRICITY:
			resId = R.drawable.electric;
			break;
		case FURNITURE:
			resId = R.drawable.furniture;
			break;
		case GLASS:
			resId = R.drawable.glass;
			break;
		case GOLD:
			resId = R.drawable.goldbar;
			break;
		case GOLDORE:
			resId = R.drawable.goldore;
			break;
		case HAPPINESS:
			resId = R.drawable.tnewhappysmiley;
			break;
		case IRON:
			resId = R.drawable.iron;
			break;
		case IRONORE:
			resId = R.drawable.ironore;
			break;
		case JEWELRY:
			resId = R.drawable.jewelry;
			break;
		case METAL:
			resId = R.drawable.metal;
			break;
		case MONEY:
			resId = R.drawable.money;
			break;
		case SAND:
			resId = R.drawable.bucketofsand;
			break;
		case SCIENCEPOINTS:
			resId = R.drawable.sciencepoints;
			break;
		case WATER:
			resId = R.drawable.water4;
			break;
		case WOOD:
			resId = R.drawable.timber;
			break;
		case OXYGEN:
			resId = R.drawable.oxygen;
			break;
		case FOOD:
			resId = R.drawable.food;
			break;
		case POPULATION:
			resId = R.drawable.population;
			break;
		case STORAGESPACE:
			resId = R.drawable.food;
			break;
		case POPULATIONCAP:
			resId = R.drawable.population;
			break;
		default:
			break;

		}
		return resId;
	}

	public static Bitmap getBitmapByResourceType(ResourceType resourceType, Context context, int width, int height)
	{
		return Bitmap.createScaledBitmap(getBitmapByResourceType(resourceType, context), width, height, false);
	}

	public static Bitmap getTileBackgroundByResourceType(ResourceType resourceType, Context context, int width,
		int height)
	{
		int id = 0;
		switch (resourceType)
		{
		case GOLDORE:
			id = R.drawable.cratergroundiron2;
			break;
		case IRONORE:
			id = R.drawable.cratergroundiron2;
			break;
		case SAND:
			id = R.drawable.sandground;
			break;
		default:
			id = R.drawable.happysmiley;
			break;
		}
		return Bitmap.createScaledBitmap(getBitmapFromResources(id, context), width, height, false);
	}

	public void loadAllResources(Context context)
	{
	}

	static class BitmapLayout
	{
		private int id;

		private Bitmap bitmap;
	}

	public static Bitmap getScaledBitmapFromResources(int backgroundID, Context context, int tileSize)
	{
		return Bitmap.createScaledBitmap(getBitmapFromResources(backgroundID, context), tileSize, tileSize, true);
	}
}
